install.packages("weatherData",repos = "http://cran.us.r-project.org")
## Warning: package 'weatherData' is not available (for R version 3.4.4)
options(repos=structure(c(CRAN="YOUR FAVORITE MIRROR")))
install.packages("leaflet")
## Warning: unable to access index for repository YOUR FAVORITE MIRROR/src/contrib:
## cannot open URL 'YOUR FAVORITE MIRROR/src/contrib/PACKAGES'
## Warning: package 'leaflet' is not available (for R version 3.4.4)
## Warning: unable to access index for repository YOUR FAVORITE MIRROR/bin/macosx/el-capitan/contrib/3.4:
## cannot open URL 'YOUR FAVORITE MIRROR/bin/macosx/el-capitan/contrib/3.4/PACKAGES'
install.packages("ggmap")
## Warning: unable to access index for repository YOUR FAVORITE MIRROR/src/contrib:
## cannot open URL 'YOUR FAVORITE MIRROR/src/contrib/PACKAGES'
## Warning: package 'ggmap' is not available (for R version 3.4.4)
## Warning: unable to access index for repository YOUR FAVORITE MIRROR/bin/macosx/el-capitan/contrib/3.4:
## cannot open URL 'YOUR FAVORITE MIRROR/bin/macosx/el-capitan/contrib/3.4/PACKAGES'
install.packages("RColorBrewer")
## Warning: unable to access index for repository YOUR FAVORITE MIRROR/src/contrib:
## cannot open URL 'YOUR FAVORITE MIRROR/src/contrib/PACKAGES'
## Warning: package 'RColorBrewer' is not available (for R version 3.4.4)
## Warning: unable to access index for repository YOUR FAVORITE MIRROR/bin/macosx/el-capitan/contrib/3.4:
## cannot open URL 'YOUR FAVORITE MIRROR/bin/macosx/el-capitan/contrib/3.4/PACKAGES'
install.packages("dplyr")
## Warning: unable to access index for repository YOUR FAVORITE MIRROR/src/contrib:
## cannot open URL 'YOUR FAVORITE MIRROR/src/contrib/PACKAGES'
## Warning: package 'dplyr' is not available (for R version 3.4.4)
## Warning: unable to access index for repository YOUR FAVORITE MIRROR/bin/macosx/el-capitan/contrib/3.4:
## cannot open URL 'YOUR FAVORITE MIRROR/bin/macosx/el-capitan/contrib/3.4/PACKAGES'
install.packages("tidyverse")
## Warning: unable to access index for repository YOUR FAVORITE MIRROR/src/contrib:
## cannot open URL 'YOUR FAVORITE MIRROR/src/contrib/PACKAGES'
## Warning: package 'tidyverse' is not available (for R version 3.4.4)
## Warning: unable to access index for repository YOUR FAVORITE MIRROR/bin/macosx/el-capitan/contrib/3.4:
## cannot open URL 'YOUR FAVORITE MIRROR/bin/macosx/el-capitan/contrib/3.4/PACKAGES'
install.packages("ggplot2")
## Warning: unable to access index for repository YOUR FAVORITE MIRROR/src/contrib:
## cannot open URL 'YOUR FAVORITE MIRROR/src/contrib/PACKAGES'
## Warning: package 'ggplot2' is not available (for R version 3.4.4)
## Warning: unable to access index for repository YOUR FAVORITE MIRROR/bin/macosx/el-capitan/contrib/3.4:
## cannot open URL 'YOUR FAVORITE MIRROR/bin/macosx/el-capitan/contrib/3.4/PACKAGES'
install.packages("readr")
## Warning: unable to access index for repository YOUR FAVORITE MIRROR/src/contrib:
## cannot open URL 'YOUR FAVORITE MIRROR/src/contrib/PACKAGES'
## Warning: package 'readr' is not available (for R version 3.4.4)
## Warning: unable to access index for repository YOUR FAVORITE MIRROR/bin/macosx/el-capitan/contrib/3.4:
## cannot open URL 'YOUR FAVORITE MIRROR/bin/macosx/el-capitan/contrib/3.4/PACKAGES'
install.packages("geosphere")
## Warning: unable to access index for repository YOUR FAVORITE MIRROR/src/contrib:
## cannot open URL 'YOUR FAVORITE MIRROR/src/contrib/PACKAGES'
## Warning: package 'geosphere' is not available (for R version 3.4.4)
## Warning: unable to access index for repository YOUR FAVORITE MIRROR/bin/macosx/el-capitan/contrib/3.4:
## cannot open URL 'YOUR FAVORITE MIRROR/bin/macosx/el-capitan/contrib/3.4/PACKAGES'
library(geosphere)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(tidyverse)
## ── Attaching packages ───────────────────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 2.2.1 ✔ readr 1.1.1
## ✔ tibble 1.4.2 ✔ purrr 0.2.4
## ✔ tidyr 0.8.0 ✔ stringr 1.3.0
## ✔ ggplot2 2.2.1 ✔ forcats 0.3.0
## ── Conflicts ──────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(ggplot2)
library(leaflet)
library(ggmap)
library(readr)
library(RColorBrewer)
severeincident <- read_csv("severe_incidents.csv")
## Parsed with column specification:
## cols(
## .default = col_character(),
## IM_INCIDENT_KEY = col_integer(),
## UNITS_ONSCENE = col_integer(),
## TOTAL_INCIDENT_DURATION = col_integer(),
## ZIP_CODE = col_integer(),
## FIRE_ORIGIN_BELOW_GRADE_FLAG = col_integer(),
## STORY_FIRE_ORIGIN_COUNT = col_integer(),
## STANDPIPE_SYS_PRESENT_FLAG = col_integer(),
## Latitude = col_double(),
## Longitude = col_double(),
## year = col_integer()
## )
## See spec(...) for full column specifications.
content <- paste("Incident Type:",severeincident$INCIDENT_TYPE_DESC,"</br>",
"Incident Time:",severeincident$INCIDENT_DATE_TIME,"</br>",
"Clear Time:",severeincident$LAST_UNIT_CLEARED_DATE_TIME,"</br>",
"Main Action Take:", severeincident$ACTION_TAKEN1_DESC)
area = colorFactor("#e31a1c",domain = severeincident$BOROUGH_DESC)
color_area = area(severeincident$BOROUGH_DESC)
map <- leaflet(severeincident) %>%
addTiles('http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png') %>%
setView(lat=40.713108, lng=-73.910728, zoom = 11)%>%
addCircles(col = color_area, popup = content)
## Assuming 'Longitude' and 'Latitude' are longitude and latitude, respectively
## Warning in validateCoords(lng, lat, funcName): Data contains 16 rows with
## either missing or invalid lat/lon values and will be ignored
map
###2a
#grouping by the type of property
severeincident <- severeincident %>% mutate(property = PROPERTY_USE_DESC)
severeincident <- separate(severeincident,property, c("ptype","ptypedetail"))
## Warning: Expected 2 pieces. Additional pieces discarded in 2308 rows [1, 2,
## 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, ...].
ptype1 <- NA
ptype1 <- ifelse(severeincident$ptype > 0 & severeincident$ptype < 200, "Assembly",ptype1)
ptype1 <- ifelse(severeincident$ptype >= 200 & severeincident$ptype < 300, "Educational Use",ptype1)
ptype1 <- ifelse(severeincident$ptype >= 300 & severeincident$ptype < 400, "Health Care", ptype1)
ptype1 <- ifelse(severeincident$ptype >= 400 & severeincident$ptype < 500, "Residential Use", ptype1)
ptype1 <- ifelse(severeincident$ptype >= 500 & severeincident$ptype < 600, "Business", ptype1)
ptype1 <- ifelse(severeincident$ptype >= 600 & severeincident$ptype < 700, "Industrial Use", ptype1)
ptype1 <- ifelse(severeincident$ptype >= 700 & severeincident$ptype < 800, "Manufaturing", ptype1)
ptype1 <- ifelse(severeincident$ptype >= 800 & severeincident$ptype < 900, "Storage", ptype1)
ptype1 <- ifelse(severeincident$ptype >= 900, "Special Property", ptype1)
ptype1 <- ifelse(is.na(ptype1), "Other Property",ptype1)
severeincident$propertytype <- ptype1
#add color
ptypec = colorFactor("Set1", domain = severeincident$propertytype)
color_ptype = ptypec(severeincident$propertytype)
#content
content2 <- paste("Property Type:",severeincident$propertytype,"</br>",
"Incident Time:",severeincident$INCIDENT_DATE_TIME,"</br>",
"Clear Time:",severeincident$LAST_UNIT_CLEARED_DATE_TIME,"</br>",
"Main Action Take:", severeincident$ACTION_TAKEN1_DESC)
map2 <- leaflet(severeincident) %>%
addTiles('http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png') %>%
setView(lat=40.713108, lng=-73.910728, zoom = 11)
map_q2 <- map2%>%
addCircles(col = color_ptype, popup = content2)
## Assuming 'Longitude' and 'Latitude' are longitude and latitude, respectively
## Warning in validateCoords(lng, lat, funcName): Data contains 16 rows with
## either missing or invalid lat/lon values and will be ignored
map_q2
###2b
mclu <- map2%>%
addCircleMarkers(col = color_ptype, popup = content2, clusterOptions = markerClusterOptions())
## Assuming 'Longitude' and 'Latitude' are longitude and latitude, respectively
## Warning in validateCoords(lng, lat, funcName): Data contains 16 rows with
## either missing or invalid lat/lon values and will be ignored
mclu
content3 <- paste("Property Type:",severeincident$propertytype,"</br>",
"Severity Level:",severeincident$UNITS_ONSCENE,"</br>",
"Incident Time:",severeincident$INCIDENT_DATE_TIME,"</br>",
"Clear Time:",severeincident$LAST_UNIT_CLEARED_DATE_TIME,"</br>",
"Main Action Take:", severeincident$ACTION_TAKEN1_DESC)
radius <- findInterval(severeincident$UNITS_ONSCENE,c(15,30,45)) * 2
severeincident <- severeincident %>% mutate(radius)
map3 <- map2 %>%
addCircleMarkers(
group = "Incident",
radius = radius,
color = color_ptype,
popup = content3,
fillOpacity = 0.5
)
## Assuming 'Longitude' and 'Latitude' are longitude and latitude, respectively
## Warning in validateCoords(lng, lat, funcName): Data contains 16 rows with
## either missing or invalid lat/lon values and will be ignored
##Firehouse
firehouse <- read_csv("FDNY_Firehouse_Listing.csv")
## Parsed with column specification:
## cols(
## FacilityName = col_character(),
## FacilityAddress = col_character(),
## Borough = col_character(),
## Postcode = col_integer(),
## Latitude = col_double(),
## Longitude = col_double(),
## `Community Board` = col_integer(),
## `Community Council` = col_integer(),
## `Census Tract` = col_integer(),
## BIN = col_integer(),
## BBL = col_double(),
## NTA = col_character()
## )
contentf <- paste("Firehouse:",firehouse$FacilityName,"</br>",
"Address:", firehouse$FacilityAddress, "</br>")
fireicon <- iconList(firehouse = makeIcon("hydrant.png"))
map3 <- map3 %>%
addMarkers(group = "Firehouse", data = firehouse, lat = firehouse$Latitude, lng = firehouse$Longitude, popup = contentf, icon = fireicon) %>%
addLayersControl(overlayGroups = c("Incident","Firehouse"),
options = layersControlOptions(collapsed = FALSE))
## Warning in validateCoords(lng, lat, funcName): Data contains 5 rows with
## either missing or invalid lat/lon values and will be ignored
map3
##append two datasets
incidentdata <- select(severeincident, IM_INCIDENT_KEY,Latitude,Longitude) %>% rename(ID = IM_INCIDENT_KEY)
firedata <- select(firehouse, FacilityName, Latitude, Longitude)
firedata <- rename(firedata, ID = FacilityName)
## calculate the distance
firedata = na.omit(firedata)
incidentdata = na.omit(incidentdata)
distance <- matrix(data = NA, nrow = nrow(incidentdata), ncol = 3)
for (i in 1:nrow(incidentdata)){
for(j in 1:nrow(firedata)){
temp <- distm(c(incidentdata$Latitude[i],incidentdata$Longitude[i]),c(firedata$Latitude[j],firedata$Longitude[j]), fun = distHaversine)
if (j==1){
mindist = temp; distance[i,1]=incidentdata$ID[i]; distance[i,2]=firedata$ID[j]; distance[i,3] = mindist}
else{
if(temp<mindist){
mindist = temp; distance[i,1]=incidentdata$ID[i]; distance[i,2]=firedata$ID[j]; distance[i,3] = mindist
}
}
}
}
# clean the data
distance <- data.frame(distance)
distance$distance <- as.numeric(distance$X3)
distance$distance <- round(distance$distance,2)
incident_with_time <-
severeincident %>%
mutate(
incident_time = lubridate::mdy_hms(INCIDENT_DATE_TIME),
arrival_time = lubridate::mdy_hms(ARRIVAL_DATE_TIME),
seconds_time_difference = arrival_time - incident_time
)
incident_with_time1 <- incident_with_time
incident_with_time1 <- incident_with_time1 %>%
select(IM_INCIDENT_KEY,BOROUGH_DESC,Latitude,Longitude,incident_time,arrival_time,seconds_time_difference)
incident_with_time1 <- na.omit(incident_with_time1)
incident_with_time1$distance <- distance$distance
# plot
plot4a <- ggplot(incident_with_time1, aes(x = distance, y = seconds_time_difference, col = BOROUGH_DESC))+
geom_point()+
ggtitle("Response time and Distance between Incident and Firehouse")+
theme_bw()+
theme(legend.position = "right",
legend.title = element_blank(),
axis.text.x = element_text(angle = -30, vjust = 1, hjust = 0))+
xlab("Distance(km)")+
ylab("Response Time(seconds)")+
scale_color_brewer(type = "qual")+
scale_x_continuous(limits = c(0,3000))+
scale_y_continuous(limits = c(0,1500))
##4b
#plot
library(RColorBrewer)
incident_with_time$seconds_time_difference <- as.numeric(incident_with_time$seconds_time_difference)
content4 <- paste("Property Type:",incident_with_time$INCIDENT_TYPE_DESC,"</br>",
"Severity Level:",incident_with_time$UNITS_ONSCENE,"</br>",
"Response Time:", incident_with_time$seconds_time_difference, "seconds","</br>",
"Distance:", incident_with_time1$distance,"km")
spectral <- brewer.pal(10, "Spectral") %>% rev()
timecolor <- colorQuantile(spectral, incident_with_time$seconds_time_difference, n = 10)
map4b <- leaflet(incident_with_time) %>%
addTiles('http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png') %>%
setView(lat=40.713108, lng=-73.910728, zoom = 11)%>%
addCircleMarkers(color = timecolor(incident_with_time$seconds_time_difference), radius = 1, popup = content4)%>%
addLegend(title = "Response Time in Second", pal = timecolor, values = incident_with_time$seconds_time_difference, opacity = 1, position = "topright")
## Assuming 'Longitude' and 'Latitude' are longitude and latitude, respectively
## Warning in validateCoords(lng, lat, funcName): Data contains 16 rows with
## either missing or invalid lat/lon values and will be ignored
plot4a
## Warning: Removed 3 rows containing missing values (geom_point).

map4b